package com.henu.yizhiya;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.henu.R;
import com.henu.xyh.MainActivity;
import com.henu.yizhiya.entity.Person;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class LoginActivity extends AppCompatActivity {

    private EditText et_login_username,et_login_pwd;
    private Button btn_login,btn_res;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //引入Bmob
        Bmob.initialize(this, "a24db0fac16b375a97072b85080ea20f");
        //获取视图
        et_login_username = (EditText) findViewById(R.id.et_login_username);
        et_login_pwd = (EditText) findViewById(R.id.et_login_pwd);
        btn_login = (Button) findViewById(R.id.btn_login);
        btn_res = (Button) findViewById(R.id.btn_res);

        Person person = BmobUser.getCurrentUser(Person.class);
        if (person!=null){
            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }

        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });


        btn_res.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
    }

    public void login(){
        if (isUserNameAndPwdValid()){
            String name = et_login_username.getText().toString().trim();
            String pwd = et_login_pwd.getText().toString().trim();

            Person person = new Person();
            person.setUsername(name);
            person.setPassword(pwd);

            person.login(new SaveListener<Person>() {
                @Override
                public void done(Person person, BmobException e) {
                    if (e==null){
                        //登陆成功
                        Toast.makeText(LoginActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
                        Person p = BmobUser.getCurrentUser(Person.class);
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        startActivity(intent);
                        finish();
                    }else {
                        //登陆失败
                        Toast.makeText(LoginActivity.this,"账号密码输入错误",Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }else {
            Toast.makeText(this,"账号或者密码输入为空",Toast.LENGTH_SHORT).show();
        }
    }

    //判断 用户名和密码输入是否为空
    public boolean isUserNameAndPwdValid() {
        if (et_login_username.getText().toString().trim().equals("")) {
            Toast.makeText(this,"",Toast.LENGTH_SHORT).show();
            return false;
        } else if (et_login_pwd.getText().toString().trim().equals("")) {
            Toast.makeText(this, "", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
}
